﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Transactions;
using System.Web;

namespace JewererBasic.BO
{
    public class ProductServiceImp
    {
        public void Create(PRODUCT product)
        {
            MSQDBEntities dc = new MSQDBEntities();

            // should remove when change database
            // remove Unit, Type when enter mould in Production Card
            // remove Qty in Product_Component
            for (int i = 0; i < product.PRODUCT_MOULD.Count; i++)
            {
                product.PRODUCT_MOULD.ElementAt(i).IdUnit = 1;
                product.PRODUCT_MOULD.ElementAt(i).IdType = 1;
            }
            for (int i = 0; i < product.PRODUCT_PROCESS.Count; i++)
            {
                product.PRODUCT_PROCESS.ElementAt(i).ProcessOrder = 1;
            }
            for (int i = 0; i < product.PRODUCT_STONE.Count; i++)
            {
                product.PRODUCT_STONE.ElementAt(i).Weight = 1;
            }
            for (int i = 0; i < product.PRODUCT_COMPONENT.Count; i++)
            {
                product.PRODUCT_COMPONENT.ElementAt(i).Qty = 1;
            }
            // end remove

            dc.PRODUCTs.Add(product);
            dc.SaveChanges();
        }

        public List<PRODUCT> QueryProductId(string query)
        {
            MSQDBEntities dc = new MSQDBEntities();
            if (string.IsNullOrEmpty(query) == true)
                return dc.PRODUCTs.ToList();
            return dc.PRODUCTs.Where(x => x.Id.Contains(query) == true).ToList();
        }

        public PRODUCT FindProduct(string id)
        {
            MSQDBEntities dc = new MSQDBEntities();
            PRODUCT product = dc.PRODUCTs.Find(id);
            return product;
        }

        public bool Update(PRODUCT product)
        {
            using (TransactionScope scope = new TransactionScope())
            {
                MSQDBEntities dc = new MSQDBEntities();

                // should remove when change database
                // remove Unit, Type when enter mould in Production Card
                // remove Qty in Product_Component
                for (int i = 0; i < product.PRODUCT_MOULD.Count; i++)
                {
                    product.PRODUCT_MOULD.ElementAt(i).IdUnit = 1;
                    product.PRODUCT_MOULD.ElementAt(i).IdType = 1;
                }
                for (int i = 0; i < product.PRODUCT_PROCESS.Count; i++)
                {
                    product.PRODUCT_PROCESS.ElementAt(i).ProcessOrder = 1;
                }
                for (int i = 0; i < product.PRODUCT_STONE.Count; i++)
                {
                    product.PRODUCT_STONE.ElementAt(i).Weight = 1;
                }
                for (int i = 0; i < product.PRODUCT_COMPONENT.Count; i++)
                {
                    product.PRODUCT_COMPONENT.ElementAt(i).Qty = 1;
                }
                // end remove

                PRODUCT realProduct = dc.PRODUCTs.Find(product.Id);
                if (product != null)
                {
                    realProduct.Drawing = product.Drawing;
                    realProduct.IdGamma = product.IdGamma;
                    realProduct.IdKind = product.IdKind;
                    realProduct.IdType = product.IdType;
                    realProduct.IdUnit = product.IdUnit;
                    realProduct.MetalWeight = product.MetalWeight;
                    realProduct.Remark = product.Remark;
                    realProduct.Size = product.Size;
                    realProduct.TotalWeight = product.TotalWeight;

                    dc.SaveChanges();

                    // Remove all component, stone, process
                    while (realProduct.PRODUCT_COMPONENT.FirstOrDefault() != null)
                        dc.PRODUCT_COMPONENT.Remove(realProduct.PRODUCT_COMPONENT.First());
                    while (realProduct.PRODUCT_MOULD.FirstOrDefault() != null)
                        dc.PRODUCT_MOULD.Remove(realProduct.PRODUCT_MOULD.First());
                    while (realProduct.PRODUCT_STONE.FirstOrDefault() != null)
                        dc.PRODUCT_STONE.Remove(realProduct.PRODUCT_STONE.First());
                    while (realProduct.PRODUCT_PROCESS.FirstOrDefault() != null)
                        dc.PRODUCT_PROCESS.Remove(realProduct.PRODUCT_PROCESS.First());
                    dc.SaveChanges();

                    // Add component, stone, process
                    foreach (var com in product.PRODUCT_COMPONENT)
                    {
                        dc.PRODUCT_COMPONENT.Add(com);
                    }
                    foreach (var pp in product.PRODUCT_PROCESS)
                    {
                        dc.PRODUCT_PROCESS.Add(pp);
                    }
                    foreach (var stone in product.PRODUCT_STONE)
                    {
                        dc.PRODUCT_STONE.Add(stone);
                    }
                    foreach (var mould in product.PRODUCT_MOULD)
                    {
                        dc.PRODUCT_MOULD.Add(mould);
                    }

                    dc.SaveChanges();
                    scope.Complete();
                    return true;
                }
            }
            return false;
        }
    }
}